package com.orangelife.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;

public class HttpUtils {

	public static Logger logger = Logger.getLogger(HttpUtils.class);

	/**
	 * 根据accessToken 获取(采用post 方法)
	 * 
	 * @param url
	 * @param jsonParam
	 * @param accessToken
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String doPsot(String url, String jsonParam, String accessToken) {
		if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonParam)) {
			return null;
		}
		String strRequest = null;
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		HttpPost httpPost = new HttpPost(url);
		try {

			post.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");

			post.addRequestHeader("accesstoken", accessToken);
			httpPost.addHeader("Content-Type", "application/json");

			post.setRequestBody(jsonParam.toString());

			int result = httpClient.executeMethod(post);
			logger.info("------------------------------------------doPost--------result--------------------------------:"
					+ result);
			System.out
					.println("-------------------------------------doPost--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ post.getResponseBodyAsString());
			}
			strRequest = post.getResponseBodyAsString();
			logger.info("------------------------------------------doPost--------result--------------------------------:"
					+ String.valueOf(result)
					+ ":"
					+ post.getResponseBodyAsString());
			System.out
					.println("------------------------------------------doPost--------result--------------------------------:"
							+ String.valueOf(result)
							+ ":"
							+ post.getResponseBodyAsString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			post.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out.println("doPost-----------------------end-----------------");
		return strRequest;

	}

	/**
	 * do 采用get 方法)
	 * 
	 * @param url
	 * @return
	 */
	public static String doGet(String url) {

		StringBuffer buffer = new StringBuffer();

		if (url == null) {
			return null;
		}

		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod(url);
		try {

			get.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");

			get.setDoAuthentication(true);
			// httpClient.getHttpConnectionManager().getParams()
			// .setSoTimeout(60000);

			int result = httpClient.executeMethod(get);

			logger.info("------------------------------------------doGet--------result--------------------------------:"
					+ result);
			System.out
					.println("-------------------------------------doGet---------result--------------------------------:"
							+ result);
			logger.debug("get method request code:" + result);
			if (result > 200) {
				return null;
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					get.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {

				buffer.append(line);
			}

			logger.info("------------------------------------------doGet--------result--------------------------------:"
					+ String.valueOf(result) + ":" + buffer.toString());
			System.out
					.println("------------------------------------------doGet--------result--------------------------------:"
							+ String.valueOf(result) + ":" + buffer.toString());
			//System.out.println(buffer.toString());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			get.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		return buffer.toString();

	}

	/**
	 * 根据accessToken 获取(采用get 方法)
	 * 
	 * @param url
	 * @param accessToken
	 * @return
	 */
	public static String doGet(String url, String accessToken) {

		if (url == null || accessToken == null) {
			return null;
		}
		String response = null;
		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod(url);
		try {

			get.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");
			get.addRequestHeader("accesstoken", accessToken);
			get.setDoAuthentication(true);
			// httpClient.getHttpConnectionManager().getParams()
			// .setSoTimeout(60000);
			int result = httpClient.executeMethod(get);
			logger.debug("get method request code:" + result);
			if (result > 200) {
				return null;
			}

			/*
			 * InputStream inputStream = get.getResponseBodyAsStream();
			 * BufferedReader br = new BufferedReader(new InputStreamReader(
			 * inputStream, "UTF-8")); StringBuffer stringBuffer = new
			 * StringBuffer(); String str = ""; while ((str = br.readLine()) !=
			 * null) { stringBuffer.append(str); }
			 */
			response = get.getResponseBodyAsString();
			logger.info("------------------------------------------doGet--------result--------------------------------:"
					+ result);
			System.out
					.println("-------------------------------------doGet---------result--------------------------------:"
							+ result);
			// response = new String(stringBuffer.toString().getBytes("UTF-8"));
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			get.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		return response;

	}

	/**
	 * 根据accessToken 获取(采用post 方法)
	 * 
	 * @param url
	 * @param jsonParam
	 * @param accessToken
	 * @return
	 */

	public static String doPut(String url, String jsonParam, String accessToken) {
		if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonParam)) {
			return null;
		}
		String strRequest = null;
		HttpClient httpClient = new HttpClient();
		PutMethod put = new PutMethod(url);
		HttpPut httpPut = new HttpPut(url);
		try {

			put.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");
			// post.setRequestHeader("Authorization", value.replace("\r\n",
			// ""));
			if (accessToken != null) {
				put.addRequestHeader("accesstoken", accessToken);
			}
			httpPut.addHeader("Content-Type", "application/json");
			int result = httpClient.executeMethod(put);
			logger.info("doPut--------result--------------------------------:"
					+ result);
			System.out
					.println("doPut--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ put.getResponseBodyAsString());
			}
			strRequest = put.getResponseBodyAsString();
			logger.info("doPut--------result--------------------------------:"
					+ String.valueOf(result) + ":"
					+ put.getResponseBodyAsString());
			System.out
					.println("doPut--------result--------------------------------:"
							+ String.valueOf(result)
							+ ":"
							+ put.getResponseBodyAsString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			put.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out
				.println("-----------------------------------doPut-----------------------end-----------------");
		return strRequest;

	}

	/**
	 * 通过urlcont进行http post请求
	 * 
	 * @author shendiao
	 * @date 20.15.03.2
	 * @param pathUrl
	 * @param json
	 * @param accessToken
	 * @return
	 */
	public static String doPostByConnection(String pathUrl, String json,
			String accessToken) {
		StringBuffer sb = new StringBuffer();
		try {

			System.out
					.println("doPost--------start--------------------------------:");
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();

			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("POST");// 设置URL请求方法
			httpConn.setInstanceFollowRedirects(true);

			// 设置请求属性
			// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致
			byte[] requestStringBytes = json.getBytes("utf-8");
			httpConn.setRequestProperty("Content-length", ""
					+ requestStringBytes.length);
			httpConn.setRequestProperty("Content-Type", "application/json");

			if (accessToken != null) {
				httpConn.setRequestProperty("accessToken", accessToken);
			}
			httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpConn.setRequestProperty("Charset", "utf-8");
			// 建立输出流，并写入数据
			OutputStream outputStream = httpConn.getOutputStream();
			outputStream.write(requestStringBytes);
			outputStream.close();
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				String readLine;
				BufferedReader responseReader;
				// 处理响应流，必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "utf-8"));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				responseReader.close();
				logger.info("-------------------------------------doPut--------result--------------------------------:"
						+ sb.toString());
				System.out
						.println("doPut--------result--------------------------------:"
								+ sb.toString() + ":" + "200");
			}
			System.out
					.println("doPost-----------------------end-----------------");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return sb.toString();

	}

	/**
	 * 通过urlcont进行http get请求
	 * 
	 * @author shendiao
	 * @date 20.15.03.2
	 * @param pathUrl
	 * @param accessToken
	 * @return
	 */
	public static String doGetByConnection(String pathUrl, String accessToken) {
		StringBuffer sb = new StringBuffer();
		try {

			System.out
					.println("dogGet--------start--------------------------------:");
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();
			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("GET");// 设置URL请求方法
			httpConn.setInstanceFollowRedirects(true);

			// 设置请求属性
			// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致

			httpConn.setRequestProperty("Content-Type", "application/json");

			if (accessToken != null) {
				httpConn.setRequestProperty("accessToken", accessToken);
				// httpConn.addRequestProperty("accessToken", accessToken);
			}
			httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpConn.setRequestProperty("Charset", "utf-8");
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				String readLine;
				BufferedReader responseReader;
				// 处理响应流，必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "utf-8"));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				responseReader.close();
				logger.info("-------------------------------------doGet--------result--------------------------------:"
						+ sb.toString());
				System.out
						.println("doGet--------result--------------------------------:"
								+ sb.toString() + ":" + "200");
			}
			System.out
					.println("doGet-----------------------end-----------------");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return sb.toString();

	}

	/**
	 * 通过urlcont进行http put请求
	 * 
	 * @author shendiao
	 * @date 20.15.03.2
	 * @param pathUrl
	 * @param json
	 * @param accessToken
	 * @return
	 */
	public static String doPutByConnection(String pathUrl, String json,
			String accessToken) {
		StringBuffer sb = new StringBuffer();
		try {

			System.out
					.println("doPut--------start--------------------------------:");
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();

			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("PUT");// 设置URL请求方法
			httpConn.setInstanceFollowRedirects(true);

			// 设置请求属性
			// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致
			byte[] requestStringBytes = json.getBytes("utf-8");
			httpConn.setRequestProperty("Content-length", ""
					+ requestStringBytes.length);
			httpConn.setRequestProperty("Content-Type", "application/json");

			if (accessToken != null) {
				httpConn.setRequestProperty("accessToken", accessToken);
			}
			httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpConn.setRequestProperty("Charset", "utf-8");
			// 建立输出流，并写入数据
			OutputStream outputStream = httpConn.getOutputStream();
			outputStream.write(requestStringBytes);
			outputStream.close();
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				String readLine;
				BufferedReader responseReader;
				// 处理响应流，必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "utf-8"));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				responseReader.close();
				logger.info("-------------------------------------doPut--------result--------------------------------:"
						+ sb.toString());
				System.out
						.println("doPut--------result--------------------------------:"
								+ sb.toString() + ":" + "200");
			}
			System.out
					.println("doPut-----------------------end-----------------");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return sb.toString();

	}

	/**
	 * 通过urlcont进行http delete请求
	 * 
	 * @author shendiao
	 * @date 20.15.03.2
	 * @param pathUrl
	 * @param accessToken
	 * @return
	 * @throws Exception
	 */
	public static String doDeleteByConnection(String pathUrl, String accessToken)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		try {

			System.out
					.println("doDelete--------start--------------------------------:");
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();
			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("DELETE");// 设置URL请求方法
			httpConn.setInstanceFollowRedirects(true);

			// 设置请求属性
			// 获得数据字节数据，请求数据流的编码，必须和下面服务器端处理请求流的编码一致

			httpConn.setRequestProperty("Content-Type", "application/json");

			if (accessToken != null) {
				httpConn.setRequestProperty("accessToken", accessToken);
			}
			httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpConn.setRequestProperty("Charset", "utf-8");
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				String readLine;
				BufferedReader responseReader;
				// 处理响应流，必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(
						httpConn.getInputStream(), "utf-8"));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				responseReader.close();
				logger.info("-------------------------------------doDelete--------result--------------------------------:"
						+ sb.toString());
				System.out
						.println("-----------------------------------------------------------doDelete--------result--------------------------------:"
								+ sb.toString() + ":" + "200");
			}
			System.out
					.println("-----------------------------------------------------------doDelete-----------------------end-----------------");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return sb.toString();

	}

	/**
	 * 根据accessToken 获取(采用get 方法)
	 * 
	 * @param url
	 * @param accessToken
	 * @return
	 */
	public static String doGetStreame(String url, String accessToken) {
		StringBuffer buffer = new StringBuffer();

		if (url == null || accessToken == null) {
			return null;
		}

		HttpClient httpClient = new HttpClient();
		GetMethod get = new GetMethod(url);
		try {
			System.out
					.println("----------------------------------------------------------doget--------result--------------------------------");
			get.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");

			if (accessToken != null) {
				get.addRequestHeader("accesstoken", accessToken);
			}
			get.setDoAuthentication(true);
			// httpClient.getHttpConnectionManager().getParams()
			// .setSoTimeout(60000);
			int result = httpClient.executeMethod(get);
			logger.debug("get method request code:" + result);
			if (result > 200) {
				return null;
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					get.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}

			System.out.println(buffer.toString());
			logger.info("-------------------------------------doGet--------result--------------------------------:"
					+ buffer.toString());
			System.out
					.println("----------------------------------------------------------doget--------result end--------------------------------");

			// response = new String(stringBuffer.toString().getBytes("UTF-8"));
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			get.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		return buffer.toString();

	}

	@SuppressWarnings("deprecation")
	public static String doPsotstreame(String url, String jsonParam,
			String accessToken) {
		if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonParam)) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();

		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		HttpPost httpPost = new HttpPost(url);
		try {

			post.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");

			if (accessToken != null) {
				post.addRequestHeader("accesstoken", accessToken);
			}
			httpPost.addHeader("Content-Type", "application/json");

			post.setRequestBody(jsonParam.toString());
			/*
			 * Gson gson = new Gson();
			 * 
			 * Map<String, Object> map = gson.fromJson(jsonParam,
			 * HashMap.class);
			 * 
			 * Set entrySet = map.entrySet(); NameValuePair[] param = new
			 * NameValuePair[entrySet.size()]; int i = 0; for (Iterator itor =
			 * entrySet.iterator(); itor.hasNext();) {
			 * 
			 * Map.Entry entry = (Map.Entry) itor.next(); param[i++] = new
			 * NameValuePair(entry.getKey().toString(), entry
			 * .getValue().toString()); }
			 * 
			 * 
			 * post.setRequestBody(param);
			 */

			// post.releaseConnection();
			// httpPost.addHeader("AccessToken", token);
			int result = httpClient.executeMethod(post);
			logger.info("-----------------------------------------------doPost--------result--------------------------------:"
					+ result);
			System.out
					.println("doPost--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ post.getResponseBodyAsString());
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			logger.info("doPost--------result--------------------------------:"
					+ String.valueOf(result) + ":"
					+ post.getResponseBodyAsString());
			System.out
					.println("doPost--------result--------------------------------:"
							+ String.valueOf(result) + ":" + buffer.toString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			post.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out.println("doPost-----------------------end-----------------");
		return buffer.toString();

	}

	/**
	 * 根据accessToken 获取(采用post 方法)
	 * 
	 * @param url
	 * @param jsonParam
	 * @param accessToken
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String doPutstreame(String url, String jsonParam,
			String accessToken) {
		StringBuffer buffer = new StringBuffer();
		if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonParam)) {
			return null;
		}
		String strRequest = null;
		HttpClient httpClient = new HttpClient();
		PutMethod put = new PutMethod(url);
		HttpPut httpPut = new HttpPut(url);
		try {
			put.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");
			if (accessToken != null) {
				put.addRequestHeader("accesstoken", accessToken);
			}
			httpPut.addHeader("Content-Type", "application/json");
			put.setRequestBody(jsonParam.toString());
			int result = httpClient.executeMethod(put);
			logger.info("-------------------------------doPut--------result--------------------------------:"
					+ result);
			System.out
					.println("---------------------------------------doPut--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ put.getResponseBodyAsString());
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					put.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			logger.info("---------------------------------------------------doPUT--------result--------------------------------:"
					+ String.valueOf(result)
					+ ":"
					+ put.getResponseBodyAsString());
			System.out
					.println("----------------------------------------------------doPUT--------result--------------------------------:"
							+ String.valueOf(result) + ":" + buffer.toString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			put.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out
				.println("-------------------------------------------------------------doPut-----------------------end-----------------");
		return strRequest;

	}

	/**
	 * 根据accessToken 获取(采用post 方法)
	 * 
	 * @param url
	 * @param jsonParam
	 * @param accessToken
	 * @return
	 */

	public static String doDeletestreame(String url, String jsonParam,
			String accessToken) {
		StringBuffer buffer = new StringBuffer();
		if (StringUtils.isBlank(url) || StringUtils.isBlank(jsonParam)) {
			return null;
		}
		String strRequest = null;
		HttpClient httpClient = new HttpClient();
		DeleteMethod delete = new DeleteMethod(url);
		HttpDelete httpDelete = new HttpDelete(url);
		try {
			delete.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/json;charset=UTF-8");
			if (accessToken != null) {
				delete.addRequestHeader("accesstoken", accessToken);
			}
			httpDelete.addHeader("Content-Type", "application/json");

			int result = httpClient.executeMethod(delete);
			logger.info("-------------------------dodelete--------result--------------------------------:"
					+ result);
			System.out
					.println("------------------------------------dodelete--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ delete.getResponseBodyAsString());
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					delete.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			logger.info("---------------------------dodelete--------result--------------------------------:"
					+ String.valueOf(result)
					+ ":"
					+ delete.getResponseBodyAsString());
			System.out
					.println("--------------------------------------dodelete--------result--------------------------------:"
							+ String.valueOf(result) + ":" + buffer.toString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			delete.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out
				.println("doDelete-----------------------end-----------------");
		return strRequest;

	}

	/**
	 * post 提交 map 格式
	 * 
	 * 
	 * 通過NameValuePair 提交方式 thhp 返回流
	 * 
	 * @param url
	 * @param map
	 * @return
	 */
	public static String doPsot(String url, Map<String, Object> map) {
		if (StringUtils.isBlank(url) || map == null) {
			return null;
		}
		String strRequest = null;
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
				"UTF-8");

		try {
			StringBuffer buffer = new StringBuffer();
			// post.setRequestHeader("Content-Type",
			// "application/json;charset=UTF-8");
			// setHeader(name, value)：如果Header中没有定义则添加，如果已定义则用新的value覆盖原用value值。
			// addHeader(name, value)：如果Header中没有定义则添加，如果已定义则保持原有value不改变。
			// post.addRequestHeader("Content-Type",
			// "application/json;charset=UTF-8");
			post.addRequestHeader(HTTP.CONTENT_TYPE,
					"application/x-www-form-urlencoded;charset=utf-8");

			NameValuePair[] parts = new NameValuePair[map.size()];
			Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
			int i = 0;
			while (it.hasNext()) {
				Map.Entry<String, Object> entry = it.next();
				parts[i++] = new NameValuePair(entry.getKey(), entry.getValue()
						.toString());
			}
			post.setRequestBody(parts);
			int result = httpClient.executeMethod(post);
			logger.info("------------------------------------------doPost--------result--------------------------------:"
					+ result);
			System.out
					.println("-------------------------------------doPost--------result--------------------------------:"
							+ result);
			if (result > 200) {
				logger.info(String.valueOf(result) + ":"
						+ post.getResponseBodyAsString());
			}
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					post.getResponseBodyAsStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}

			System.out.println(buffer.toString());
			strRequest = buffer.toString();
			logger.info("------------------------------------------doPost--------result--------------------------------:"
					+ String.valueOf(result)
					+ ":"
					+ post.getResponseBodyAsString());
			System.out
					.println("------------------------------------------doPost--------result--------------------------------:"
							+ String.valueOf(result)
							+ ":"
							+ post.getResponseBodyAsString());

		} catch (Exception e) {
			System.out.println(e.getMessage());
			logger.debug(e.getMessage());
			e.printStackTrace();
		} finally {
			post.releaseConnection();
			httpClient.getHttpConnectionManager().closeIdleConnections(0);
		}
		System.out.println("doPost-----------------------end-----------------");
		return strRequest;

	}

	public static InputStream doGetByConnection(String pathUrl) {
		InputStream inputStream = null;
		try {

			System.out
					.println("dogGet--------start--------------------------------:");
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url
					.openConnection();
			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("GET");// 设置URL请求方法
			httpConn.setInstanceFollowRedirects(true);
			// httpConn.setRequestProperty("Content-Type", "application/json");
			httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
			httpConn.setRequestProperty("Charset", "utf-8");
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();
			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功

				inputStream = httpConn.getInputStream();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return inputStream;

	}
}
